<template>
  <div id="app">
    <keep-alive>
      <component :is="layout">
      </component>
    </keep-alive>
  </div>
</template>

<script type="text/javascript">
  import BlankLayout from "./layout/BlankLayout.vue";
  import NavLayout from "./layout/NavLayout.vue"
  import NavLayout2 from "./layout/NavLayout2.vue"
  import NavCar from "./layout/NavLayoutCar.vue"
  import NavDocument from "./layout/NavLayoutDocument.vue"
  export default {
    components: {
      "nav-layout": NavLayout, 
      "nav-layout2": NavLayout2,
      "nav-document": NavDocument,
      "nav-car": NavCar,                              
      "blank-layout": BlankLayout,
    },
    props: {
      layout: {
        type: String,
        default: "nav-layout"
      }
    },
    watch: {
      '$route': {
        handler(newV, oldV) {
          this.checkActive()
          if(!oldV.name && oldV.path == '/' ) { // 判断初次进入, 执行登录检查
            this.setEntry(newV.query)
            if(newV.name == 'about') {
              return false
            }
            let config = {
              url: `${this.$common.basePath}meeting.php`,
              data: {
                do: 'login',
                user_id: this.$route.query.userid,
                token: this.$route.query.token
              },
              method: 'POST'
            }
            this.$axios(config).then(res => {
              
            }).catch(err => {
              this.$toast('用户登录信息获取失败')
              this.$bridge.callHandler('backToLogin')
            })
          }
        },
        deep: true
      }
    },
    data () {
      return {
      }
    },
    created() {
      window.vueGlobal = this 
      this.initBridge()
    },
    mounted() {

    },
    methods: {
      /**
       * 将入口信息写入store
       * @param {[type]} query [description]
       */
      setEntry(query) {
        this.$store.commit('changeStatus', {key: 'from', value: query.from ? query.from : 'default'})
        this.$store.commit('changeStatus', {key: 'token', value: query.token ? query.token : ''})
        this.$store.commit('changeStatus', {key: 'userId', value: query.userid ? query.userid : 1})
      },
      /**
       * 修改激活的导航
       * @return {[type]} [description]
       */
      checkActive() {
        switch(this.$route.path) {
          case '/meetingManage':
            this.$store.commit('changeStatus', {key: 'activeNav', value: 0})
            break;
          case '/approvalMeeting':
            this.$store.commit('changeStatus', {key: 'activeNav', value: 1})
            break;
          case '/myMeeting':
            this.$store.commit('changeStatus', {key: 'activeNav', value: 2})
            break;
          default:
            this.$store.commit('changeStatus', {key: 'activeNav', value: 0})
            break;
        }
      },
      /**
       * 初始化bridge 注册响应事件
       * 注册 原生点击返回按钮的 响应回调
       */
      initBridge() {
        let self = this
        // 在此全局调用 每当原生调用此方法 历史回滚
        self.$bridge.registerHandler('navBackClicked', (data, responseCallback)  => {
          console.log("原生触发了 navBackClicked 事件：")
          self.$router.go(-1)
        })
      }
    }
  }
</script>
